//
//  BF_TableView.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-10-30.
//
//

#import "JddBasketBall_SFC_TableView.h"
#define CELL_HEIGHT 75
#define SECOND_CELL_HEIGHT 90
@interface JddBasketBall_SFC_TableView ()

@end

@implementation JddBasketBall_SFC_TableView
- (void)viewDidLoad {
	[super viewDidLoad];
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    self.tableView.tableFooterView = v;
    [self.tableView setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
	groupArray = [[NSMutableArray alloc] init];
	btnTitles = [[NSMutableArray alloc] init];
    
	secondCellDataArray = [[NSMutableArray alloc] init];
	for (int i = 0; i < [_tableData count]; ++i) {
		JddFollowNumber *follownum = [[JddFollowNumber alloc]init];
		follownum.open = 1;
		[groupArray addObject:follownum];
	}
    if (groupArray.count == 0) {
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    }

}

- (void)sportTeamButtonHeadClicked:(NSNumber *)position {
	JddFollowNumber *groupDic = [groupArray objectAtIndex:[position intValue]];
	//设置groupDic中的展开状态
	if (groupDic.open == 0) {
		groupDic.open = 1;
	}
	else {
		groupDic.open = 0;
	}
	[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[position intValue]]
	              withRowAnimation:UITableViewRowAnimationFade];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [_tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSDictionary *sectionCell = [_tableData objectAtIndex:section];
	NSString *MCount = [sectionCell objectForKey:@"MCount"];
	if ([groupArray count] > 0) {
		JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
		if (groupDic.open == 0) {
			return 0;
		}
		else {
			return [MCount intValue];
		}
	}
	else {
		return [MCount intValue];
	}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	CGFloat h = CELL_HEIGHT;
	JddBF_TableViewSecondCellIData *sc = [self getSecondCellDataArrayWithIndexPath:indexPath];
	if (sc) {
		if (sc.isOpen) {
			h += SECOND_CELL_HEIGHT;
		}
	}
	return h;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	return 34.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	JddPickNumSportItemCellHeader *cell = [JddPickNumSportItemCellHeader getInstanceView];
	NSDictionary *sectionCell = [_tableData objectAtIndex:section];
	NSNumber *MCount = [sectionCell objectForKey:@"MCount"];
	NSString *Name = [sectionCell objectForKey:@"issue"];
	cell.day.text = Name;
	cell.Date.text = [JddCommonFunction convertWeekByNumber:[sectionCell objectForKey:@"Wk"]];
	cell.Mnum.text = [NSString stringWithFormat:@"%d场比赛可投", [MCount intValue]];
	cell.statePosition = [NSNumber numberWithInt:section];
	cell.delegate = self;
	if ([groupArray count] > 0) {
		JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
		if (groupDic.open == 0) {
			cell.HeadButton.image = [UIImage imageNamed:@"hemai_hall_down.png"];
		}
		else {
			cell.HeadButton.image = [UIImage imageNamed:@"hemai_up.png"];
		}
	}
	return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	NSDictionary *sectionCell = [_tableData objectAtIndex:indexPath.section];
	NSArray *Match = [sectionCell objectForKey:@"Matches"];
	NSDictionary *MatchCell = [Match objectAtIndex:indexPath.row];
	NSString *ETime = [MatchCell objectForKey:@"ETime"];
	NSString *HTeam = [MatchCell objectForKey:@"HTeam"];
	NSString *MID = [MatchCell objectForKey:@"MID"];
	NSString *NMm = [MatchCell objectForKey:@"NMm"];
	NSString *VTeam = [MatchCell objectForKey:@"VTeam"];
    
	JddPickNumBasketBallCellSFC *cell = [JddPickNumBasketBallCellSFC getInstanceView];
    
	cell.indexPath = indexPath;
	cell.delegate = self;
    
	cell.weakNum.text = [NSString stringWithFormat:@"%@", [MID substringFromIndex:6]];
    //截止
	cell.EndTime.text = [NSString stringWithFormat:@"%@", [ETime substringWithRange:NSMakeRange(11, 5)]];
	cell.Match.text = NMm;
	cell.HTeam.text = VTeam;
	cell.VTeam.text = HTeam;
	cell.MID = MID;
    
	cell.aSportNumber = [[JddSportNumber alloc] init];
	cell.aSportNumber.code = MID;
	cell.aSportNumber.Hteam = HTeam;
	cell.aSportNumber.Vteam = VTeam;
	cell.aSportNumber.endTime = ETime;
	cell.aSportNumber.teamArray = [[NSMutableArray alloc] init];
    
	//记录选中状态
	[self setCellBtnSelectWithCell:cell];
	//保存cell的btn标题
	[self setCellBtnTitleWithCell:cell];
	return cell;
}

- (void)setCellBtnTitleWithCell:(JddPickNumBasketBallCellSFC *)cell {
	BOOL cellBtnTitleExist = NO;
	for (JddBFCellBtnsData *b in btnTitles) {
		if ([b.indexPath isEqual:cell.indexPath]) {
			cellBtnTitleExist = YES;
			//比分
			if (b.windTitles.count == 0) {
				[cell.ksBtn setTitle:@"客胜" forState:UIControlStateNormal];
			}
			else if (b.windTitles.count == 1) {
				[cell.ksBtn setTitle:[b.windTitles objectAtIndex:0] forState:UIControlStateNormal];
			}
			else {
				[cell.ksBtn setTitle:@"负多个" forState:UIControlStateNormal];
			}
			if (b.loseTitles.count == 0) {
				[cell.zsBtn setTitle:@"主胜" forState:UIControlStateNormal];
			}
			else if (b.loseTitles.count == 1) {
				[cell.zsBtn setTitle:[b.loseTitles objectAtIndex:0] forState:UIControlStateNormal];
			}
			else {
				[cell.zsBtn setTitle:@"胜多个" forState:UIControlStateNormal];
			}
		}
	}
    
	if (!cellBtnTitleExist) {
		JddBFCellBtnsData *b = [[JddBFCellBtnsData alloc] init];
		b.indexPath = cell.indexPath;
		b.windTitles = [[NSMutableArray alloc] init];
		b.loseTitles = [[NSMutableArray alloc] init];
		[btnTitles addObject:b];
	}
}

- (void)setCellBtnSelectWithCell:(JddPickNumBasketBallCellSFC *)cell {
	JddBF_TableViewSecondCellIData *sc = [self getSecondCellDataArrayWithIndexPath:cell.indexPath];
	cell.ksBtn.selected = NO;
	cell.zsBtn.selected = NO;
    
	if (sc) {
		if (sc.isOpen) {
			if (sc.currentTag == 0) {
				cell.ksBtn.selected = YES;
			}
			else {
				cell.zsBtn.selected = YES;
			}
		}
	}
	[self addSecondCellWithBF_TableViewSecondCellIData:sc AndTag:sc.currentTag AndIndexPath:cell.indexPath And:cell];
}

- (void)openCell:(UIButton *)btn :(NSIndexPath *)indexPath {
	int bTag = btn.tag;
	JddBF_TableViewSecondCellIData *sc = [self setSecondCellDataArrayWithIndexPath:indexPath];
	if (bTag == sc.currentTag) {
		sc.isOpen = !sc.isOpen;
	}
	else {
		sc.isOpen = YES;
	}
	sc.currentTag = bTag;
	UITableViewRowAnimation rowAnimation = UITableViewRowAnimationAutomatic;
	NSMutableArray *updataIndexPathArray = [NSMutableArray array];
	[updataIndexPathArray addObject:indexPath];
	[self.tableView reloadRowsAtIndexPaths:updataIndexPathArray withRowAnimation:rowAnimation];
	[self addSecondCellWithBF_TableViewSecondCellIData:sc AndTag:bTag AndIndexPath:indexPath And:nil];
}

//执行添加第2个cell
- (void)addSecondCellWithBF_TableViewSecondCellIData:(JddBF_TableViewSecondCellIData *)sc AndTag:(int)bTag AndIndexPath:(NSIndexPath *)indexPath And:(JddPickNumBasketBallCellSFC *)cell2 {
	if (sc.isOpen) {
		JddPickNumBasketBallCellSFC *cell = (JddPickNumBasketBallCellSFC *)[self.tableView cellForRowAtIndexPath:indexPath];
		if (cell2 != nil) {
			cell = cell2;
		}
		UIView *viewForExpand = [sc.cells objectAtIndex:bTag];
		viewForExpand.frame = CGRectMake(0, CELL_HEIGHT, cell.bounds.size.width, 0);
		[UIView transitionWithView:cell
		                  duration:0.3
		                   options:UIViewAnimationOptionCurveEaseOut
		                animations: ^
         {
            [viewForExpand removeFromSuperview];
            [cell addSubview:viewForExpand];
            viewForExpand.frame = CGRectMake(0, CELL_HEIGHT, cell.bounds.size.width, SECOND_CELL_HEIGHT);
         }
         
		                completion: ^(BOOL finished)
         {
         //需要处理
         //         [self handleCellOpenThanTheViewWithIndexPath:indexPath AndTag:bTag];
         }
         
         ];
	}
}

//处理打开Cell的时候。cell中的内容超出了列表的Fram
- (void)handleCellOpenThanTheViewWithIndexPath:(NSIndexPath *)indexPath AndTag:(int)tag {
	UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
	CGFloat h = CELL_HEIGHT;
	h += SECOND_CELL_HEIGHT;
	CGPoint p = [cell convertPoint:CGPointZero fromView:self.view];
    
	if ((h - p.y) > self.view.frame.size.height) {
		[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
	}
}

- (JddBF_TableViewSecondCellIData *)setSecondCellDataArrayWithIndexPath:(NSIndexPath *)indexPath {
	BOOL isExist = NO;
	JddBF_TableViewSecondCellIData *sc;
	for (JddBF_TableViewSecondCellIData *s in secondCellDataArray) {
		if ([s.indexPath isEqual:indexPath]) {
			sc = s;
			isExist = YES;
			break;
		}
	}
	if (!isExist) {
		sc = [[JddBF_TableViewSecondCellIData alloc] init];
		sc.indexPath = indexPath;
		UITableViewCell *cell1 = [self getSecondCellWithTag:0 AndIndexPath:indexPath];
		UITableViewCell *cell3 = [self getSecondCellWithTag:1 AndIndexPath:indexPath];
		sc.cells = [NSMutableArray arrayWithObjects:cell1, cell3, nil];
		[secondCellDataArray addObject:sc];
	}
	return sc;
}

- (JddBF_TableViewSecondCellIData *)getSecondCellDataArrayWithIndexPath:(NSIndexPath *)indexPath {
	JddBF_TableViewSecondCellIData *sc;
	for (JddBF_TableViewSecondCellIData *s in secondCellDataArray) {
		if ([s.indexPath isEqual:indexPath]) {
			sc = s;
			break;
		}
	}
	return sc;
}

//二级cell
- (UITableViewCell *)getSecondCellWithTag:(int)tag AndIndexPath:(NSIndexPath *)indexPath {
	NSDictionary *sectionCell = [_tableData objectAtIndex:indexPath.section];
	NSArray *Match = [sectionCell objectForKey:@"Matches"];
	NSDictionary *MatchCell = [Match objectAtIndex:indexPath.row];
    
	JddPickNumBasketBallCellSFC *parentCell = (JddPickNumBasketBallCellSFC *)[self.tableView cellForRowAtIndexPath:indexPath];
	NSArray *spcbf = [[MatchCell objectForKey:@"SpSFC"] componentsSeparatedByString:@"|"];
    
	JddPickNumBasketBallSFCOpenSecondCell *cell = [JddPickNumBasketBallSFCOpenSecondCell getInstanceView];
	cell.aSportNumber = parentCell.aSportNumber;
	cell.parentBtnTag = tag;
	cell.delegate = self;
	cell.indexPath = indexPath;
	//客
	if (tag == 0) {
		cell.btn1DownText.text = [spcbf objectAtIndex:0];
		cell.btn2DownText.text = [spcbf objectAtIndex:1];
		cell.btn3DownText.text = [spcbf objectAtIndex:2];
		cell.btn4DownText.text = [spcbf objectAtIndex:3];
		cell.btn5DownText.text = [spcbf objectAtIndex:4];
		cell.btn6DownText.text = [spcbf objectAtIndex:5];
	}
	//主
	else {
		cell.btn1DownText.text = [spcbf objectAtIndex:6];
		cell.btn2DownText.text = [spcbf objectAtIndex:7];
		cell.btn3DownText.text = [spcbf objectAtIndex:8];
		cell.btn4DownText.text = [spcbf objectAtIndex:9];
		cell.btn5DownText.text = [spcbf objectAtIndex:10];
		cell.btn6DownText.text = [spcbf objectAtIndex:11];
	}
	return cell;
}

- (void)setBFOpenCellBtnTitlsWithIndexPath:(NSIndexPath *)indexPath AndButton:(UIButton *)button {
	JddPickNumBasketBallCellSFC *cell = (JddPickNumBasketBallCellSFC *)[self.tableView cellForRowAtIndexPath:indexPath];
	NSString *btnTitle = button.titleLabel.text;
	for (JddBFCellBtnsData *b in btnTitles) {
		if ([b.indexPath isEqual:indexPath]) {
			if (cell.ksBtn.selected) {
				if (button.selected) {
					[b.windTitles addObject:btnTitle];
				}
				else {
					[b.windTitles removeObject:btnTitle];
				}
				if (b.windTitles.count == 0) {
					[cell.ksBtn setTitle:@"客胜" forState:UIControlStateNormal];
				}
				else if (b.windTitles.count == 1) {
					[cell.ksBtn setTitle:[b.windTitles objectAtIndex:0] forState:UIControlStateNormal];
				}
				else {
					[cell.ksBtn setTitle:@"负多个" forState:UIControlStateNormal];
				}
			}
			else if (cell.zsBtn.selected) {
				if (button.selected) {
					[b.loseTitles addObject:btnTitle];
				}
				else {
					[b.loseTitles removeObject:btnTitle];
				}
				if (b.loseTitles.count == 0) {
					[cell.zsBtn setTitle:@"主胜" forState:UIControlStateNormal];
				}
				else if (b.loseTitles.count == 1) {
					[cell.zsBtn setTitle:[b.loseTitles objectAtIndex:0] forState:UIControlStateNormal];
				}
				else {
					[cell.zsBtn setTitle:@"胜多个" forState:UIControlStateNormal];
				}
			}
		}
	}
}

- (void)sportTeamButtonClicked:(NSArray *)arr number:(JddSportNumber *)number1 {
	NSIndexPath *indexPath = [arr objectAtIndex:1];
	UIButton *button = [arr objectAtIndex:0];
	JddSportNumber *number = [[JddSportNumber alloc] init];
	number.code = number1.code;
	number.teamArray = [NSMutableArray arrayWithArray:number1.teamArray];
	number.endTime = number1.endTime;
	number.Hteam = number1.Hteam;
	number.Vteam = number1.Vteam;
	if (![self.sportlotterylist BallHasNumber:number]) {
		[button setSelected:NO];
	}
	else {
		if ([self.sportlotterylist.sportList count] > 15) {
			[self.sportlotterylist BallHasNumber:number];
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"最多选择15场比赛" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
			[alert show];
			[button setSelected:NO];
		}
		else {
			[button setSelected:YES];
		}
	}
	[self setBFOpenCellBtnTitlsWithIndexPath:indexPath AndButton:button];
	[self.delegate performSelector:@selector(selectMatch)];
}

@end
